//
//  ME.m
//  HYHOME
//
//  Created by Mac on 16/1/20.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ME.h"
#import "RESideMenu.h"
#import "ZCViewController.h"
#import "Personal.h"
@interface ME ()<UITextFieldDelegate>


@property (nonatomic,retain)UITextField *user;
@property (nonatomic,retain)UITextField *password;

@property (nonatomic,retain)UIImageView *backgroundview;
@property (nonatomic,retain)UIImageView *zhuceview;

@end

@implementation ME

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{


    self.navigationItem.title = @"用户登录";
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    return self;
    
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
     self.view.backgroundColor = [UIColor whiteColor];
     self.backgroundview= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"443d32a40e911e040266efb72e72699c.jpeg"]];
     self.backgroundview.frame = self.view.frame;
    
      [self.view addSubview: self.backgroundview];

    [self.backgroundview release];
    
    //开启用户交互
    self.backgroundview.userInteractionEnabled = YES;
    
  
    
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 40)];
    name.text = @"用户名:";
    
    self.user = [[UITextField alloc]initWithFrame:CGRectMake(130, 100, self.view.frame.size.width - 180, 35)];
    self.user.borderStyle = 3;
    self.user.placeholder = @"请输入用户名";
    [self.backgroundview addSubview:name];
    [self.backgroundview addSubview:self.user];
    [self.user release];
    [name release];
    
    
    
    UILabel *password  = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, self.view.frame.size.width - 100, 40)];
    password.text = @"密  码:";
    self.password = [[UITextField alloc]initWithFrame:CGRectMake(130, 150, self.view.frame.size.width - 180, 35)];
    self.password.placeholder = @"请输入密码";
    self.password.borderStyle = 3;
    
    [self.backgroundview addSubview:self.password];
    [self.backgroundview addSubview:password];
    [password release];
    [self.password release];
    


    
    
    
    self.user.delegate = self;
    self.password.delegate = self;
    
    
    
    UIButton *buttonLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonLogin setFrame:CGRectMake(90, 200, 50, 55)];
    [buttonLogin setTitle:@"登录" forState:UIControlStateNormal];
    [self.backgroundview addSubview:buttonLogin];
    [buttonLogin addTarget:self action:@selector(LoginAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonzhuce = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonzhuce setFrame:CGRectMake(200, 200, 50, 55)];
    [buttonzhuce setTitle:@"注册" forState:UIControlStateNormal];
    [self.backgroundview addSubview:buttonzhuce];

    [buttonzhuce addTarget:self action:@selector(zhuce:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 240, self.view.frame.size.width, 30)];

    label.text = @"------------第三方登录------------";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [self.backgroundview addSubview:label];
    

}
- (void)click:(UIBarButtonItem *)sender{
    //结束编辑
    
    [self.sideMenuViewController presentLeftMenuViewController];

    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)LoginAction:(UIButton *)button{
    
    // 判断用户名和密码是否为空
    if (self.user.text == nil ||[self.user.text isEqualToString:@""] || _password.text == nil || [_password.text isEqualToString:@""] ) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        
        
                UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
        
        
                [alert addAction:defaultaction];
                [self presentViewController:alert animated:YES completion:nil];
    }else{
        
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                NSString *userName = [user objectForKey:@"name"];
                NSString *userPassword = [user objectForKey:@"password"];
        //

    
        if ([userName isEqualToString:self.user.text] && [userPassword isEqualToString:_password.text]) {
            
            
                        //        //登陆成功
                        [user setBool:YES forKey:@"loginState"];
            
                        [self dismissViewControllerAnimated:YES completion:nil];
            
                        Personal  *set = [[Personal alloc]init];
            
                        [self.navigationController pushViewController:set animated:YES];
            
                    }else{
            
                        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *defaultaction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }];
                        
                        
                        [alert2 addAction:defaultaction2];
                        [self presentViewController:alert2 animated:YES completion:nil];
                                                         
                    }
                                
        
    }
    
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)zhuce:(UIButton *)button{

    ZCViewController *zc = [[ZCViewController alloc]init];
    [self.navigationController pushViewController: zc animated:YES];
    


}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    
    return YES;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
